<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('api服务列表')" />
	<th:block th:include="include :: layout-latest-css" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">

	<div class="ui-layout-west">
		<div class="box box-main">
			<div class="box-header">
				<div class="box-title">
					<i class="fa icon-grid"></i> 服务组
				</div>
				<div class="box-tools pull-right">
				    <a type="button" class="btn btn-box-tool" href="#" onclick="addGroup()" title="添加组"><i class="fa fa-plus"></i></a>
					<button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新"><i class="fa fa-refresh"></i></button>
				</div>
			</div>
			<div class="ui-layout-content">
				<div id="tree" class="ztree"></div>
			</div>
		</div>
	</div>

	<div class="ui-layout-center">
		<div class="container-div">
			<div class="row">
				<div class="col-sm-12 search-collapse">
					<form id="formId">
						<input type="hidden" name="apiGroupId" id="apiGroupId"/>
						<div class="select-list">
							<ul>
								<li>
									api名称：<input type="text" name="apiName"/>
								</li>
	
								<li>
									api地址：<input type="text" name="apiUrlSuffix"/>
								</li>
	
								<li>
									<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
									<a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
				
		        <div class="btn-group-sm" id="toolbar" role="group">
					<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="api:url:add">
						<i class="fa fa-plus"></i> 添加
					</a>
					<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="api:url:edit">
						<i class="fa fa-edit"></i> 修改
					</a>
					<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="api:url:remove">
						<i class="fa fa-remove"></i> 删除
					</a>
					<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="api:url:export">
							<i class="fa fa-download"></i> 导出
					 </a>
				</div>
				<div class="col-sm-12 select-table table-striped">
					<table id="bootstrap-table" data-mobile-responsive="true"></table>
				</div>
			</div>
		</div>
	</div>
	
    <div th:include="include :: footer"></div>
    <th:block th:include="include :: layout-latest-js" />
	<th:block th:include="include :: ztree-js" />
	
    <script th:inline="javascript">
    	var paramFlag = [[${@permission.hasPermi('api:param:view')}]];
        var editFlag = [[${@permission.hasPermi('api:url:edit')}]];
        var removeFlag = [[${@permission.hasPermi('api:url:remove')}]];
        var prefix = ctx + "api/apiUrl";

        $(function() {
		    var panehHidden = false;
		    if ($(this).width() < 769) {
		        panehHidden = true;
		    }
		    $('body').layout({ initClosed: panehHidden, west__size: 185 });
		    queryServerList();
		    queryGroupTree();
		});
        
        $('#btnRefresh').click(function() {
        	queryGroupTree();
        	$("#apiGroupId").val("");
			$.table.search();
		});
        
        function addGroup(){
        	$.modal.open("添加服务组", ctx + "api/apiGroup/add");
        }
        
        function queryGroupTree()
		{
			var url = ctx + "api/apiGroup/treeData";
			var options = {
		        url: url,
		        expandLevel: 2,
		        callback: {
			        onClick: zOnClick,                      // 用于捕获节点被点击的事件回调函数
			        beforeRemove:beforeRemove,//点击删除时触发，用来提示用户是否确定删除（可以根据返回值 true|false 确定是否可以删除）
	                beforeEditName: beforeEditName,//点击编辑时触发，用来判断该节点是否能编辑
	                beforeDrag:beforeDrag,//用户禁止拖动节点
		        },
		        edit: {
	                enable: true,
	                editNameSelectAll:true,
	                removeTitle:'删除',
	                renameTitle:'重命名'
	            }
		    };
			$.tree.init2(options);
			
			function zOnClick(event, treeId, treeNode) {
				$("#apiGroupId").val(treeNode.id);
				$.table.search();
			}
			
			function beforeRemove(e,treeNode){
				if(confirm("确定要删除吗？将会把此组中的服务移动到默认组中！")){
					$.ajax({
					    type:'post',
						url:ctx + "api/apiGroup/remove",
						data:{'ids':treeNode.id},
						dataType:"json",
						async:false,
						success: function (result) {
							$.table.search();
        					if(result.code==0){
        						return true;
        					}else{
        						return false;
        					}
        				}
					});
				}else{
					return false;
				}
		    }
		    function beforeEditName(treeId,treeNode){
		    	$.modal.open("修改分组", ctx+"api/apiGroup/edit/"+treeNode.id);
		    	return false;
		    }
		    function beforeDrag(treeId,treeNodes){
		        return false;
		    }
		}
        
        function queryServerList() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "api服务",
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'apiId', 
					title : '主键',
					visible: false
				},
				{
					field : 'apiName', 
					title : '服务名称',
					sortable: true
				},
// 				{
// 					field : 'serverHead', 
// 					title : '服务地址头',
// 					sortable: true
// 				},
// 				{
// 					field : 'serverDomain', 
// 					title : '服务域名/ip',
// 					sortable: true
// 				},
// 				{
// 					field : 'serverSuffix', 
// 					title : '服务地址访问后缀',
// 					sortable: true
// 				},
				{
					field : 'apiUrlSuffix', 
					title : 'api地址',
					sortable: true
				},
// 				{
// 					field : 'requestUrl', 
// 					title : '真实请求地址',
// 					sortable: true
// 				},
				{
					field : 'apiRequestMethod', 
					title : '请求方式',
					sortable: true
				},
// 				{
// 					field : 'apiServerRequestWay', 
// 					title : '负载均衡方式',
// 					sortable: true,
// 					formatter: function(value, row, index) {
// 						return value;
// 		            }
// 				},
				{
					field : 'remark', 
					title : '备注',
					formatter: function(value, row, index) {
						if(value!=null && value.length>15){
							value = "<span title='"+value+"'>"+value.substring(0,15)+"。。。</span>";
						}
						return value;
		            }
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];                               
		            	actions.push('<a class="btn btn-success btn-xs ' + paramFlag + '" href="javascript:void(0)" onclick="editParam(\'' + row.apiUrlSuffix + '\',\'' + row.apiName + '\',\'' + row.apiId + '\')"><i class="fa fa-edit"></i>参数</a> ');
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.apiId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.apiId + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        }
        
        function editParam(apiUrlSuffix, apiName, apiId){
        	if(apiUrlSuffix.indexOf("*") != -1){
        		layer.msg("*匹配模式不需要配置参数验证！",{icon:1});
        	}else{
        		$.modal.openTab("配置api["+apiName+"]的参数", ctx + "api/apiUrlParam?apiId="+apiId);
        	}
        	
        }
        
        function reset() {
        	var currentId = "formId";
        	$("#" + currentId)[0].reset();
        	$("#apiGroupId").val("");
            $.btTable.bootstrapTable('refresh');
        }
    </script>
</body>
</html>